<template name="InvitationCode">
	<div>
		<!-- 输入邀请码 -->
		<u-modal :show="invitationCode_show" confirmText="取消" title="邀请码" width="700rpx" @confirm="cancelInput">
			<view class="slot-content">
                <u-code-input v-model="invitationCode" mode="line" :maxlength="6" @finish="finish"></u-code-input>
			</view>
		</u-modal>
        <u-toast ref="uToast"></u-toast>
	</div>
</template>

<script>
export default {
	name: "InvitationCode",
	data() {
		return {
			invitationCode_show: false,
            invitationCode: '',
            errorCodeTimes: 0
		};
	},

  methods: {

	/**
	 * 判断是否缓存中是否已经存在邀请码，如果不存在则弹出输入框，如果存在则不弹出输入框并回调子页面的方法
     */
	isExistInvitationCode() {
		this.checkUser()
		
	},
	
	/** 
     * 输入验证码
     * @param {Object} e
     */
    async finish(e) {
        let result = await this.$u.api.unifyMiniRest({
            systemid: "meily",
            url: "/space/accept/verify",
            userId: uni.getStorageSync('userInfo').userId,
            nickName: uni.getStorageSync('userInfo').nickName,
            avatar: uni.getStorageSync('userInfo').avatarUrl,
            shareId: e,
            loading: true // 默认发起请求会有一个全局的Loading，设置false可去掉
        });
        if (result.code == 200) {
            this.checkUser()
        }else{
            this.invitationCode = ''
            this.errorCodeTimes += 1
            if(this.errorCodeTimes == 3){
                this.showMSG('none', '邀请码错误已达3次！')
                // this.$refs.uToast.show({
                //     message: '邀请码错误已达3次！',
                //     type: 'error',
                //     duration: 1500
                // })
                setTimeout(() => {
                    uni.reLaunch({url: '/pages/index/index'})
                }, 2000)
            }else{
                this.showMSG('error', '邀请码错误！')
                // this.$refs.uToast.show({
                //     message: '邀请码错误！',
                //     type: 'error',
                //     duration: 1500
                // })
            }
        }
    },

    /**
     * 验证当前用户是否为会员
     */
    async checkUser() {
        let _this = this
        let result = await _this.$u.api.unifyMiniRest({
            systemid: "meily",
            url: "/space/accept/checkUser",
            userId: uni.getStorageSync('userInfo').userId,
            loading: true // 默认发起请求会有一个全局的Loading，设置false可去掉
        });
        if (result.code == 200) {
            _this.invitationCode_show = false
            //将邀请码存入缓存，并调用子页面方法
            uni.setStorage({
                key: 'InvitationCode',
                data: true,
                success: function () {
                    _this.$emit('childEvent', '');
                }
            })
        }else{
            uni.getStorage({
                key: 'InvitationCode',
                success: function (res) {
                    if (res.data) {
                        _this.$emit('childEvent', '');
                    } else {
                        _this.invitationCode_show = true;
                    }
                },
                fail: function (res) {
                    _this.invitationCode_show = true;
                }
            })
        }
    },

    /** 
     * 取消输入邀请码
     */
    cancelInput() {
        this.invitationCode_show = false;
    },

  },
};
</script>

<style scoped>
.slot-content {
    font-size: 28rpx;
    color: #bcbcbc;
    text-align: center;
    margin: 0 auto;
    padding-top: 60rpx;
    padding-bottom: 80rpx;
}
</style>
